<?php

class Ad_Con_Normal {

    public $view;
    public $page;
    public static $a_conf = array(2000000, array('jpg', 'png'));

    function __construct() {
        ad_controller::get_view();
        $this->view = new View(DEF_AD_URL, DEF_AD_PATH, 'default', 'en', array(
            'web_name' => 'ADMIN PAGE',
            'web_img' => '',
            'web_title' => 'ADMIN PAGE',
            'web_discription' => '',
            'web_keyword' => '',
            'robot' => -2,
            'css' => array('S_Ad_Style', 'S_Table', 'S_Upload'),
            'js' => array('con_ajax')
        ));
        $this->view->head();
    }

    function home($act = '') {
        if (isset($_SESSION['ad_id'])) {
            $this->home_view($act);
        } else {
            $this->login();
        }
    }

    public function login() {
        $data = array('error' => '');
        if (isset($_POST['log_ad'])) {
            if (!empty($_POST['user']) && !empty($_POST['pass'])) {
                ad_controller::get_model('ad_login');
                try {
                    $al = new ad_login();
                    $us = $al->ad_login($_POST['user'], $_POST['pass']);
                    if ($us != -1) {
                        $_SESSION['ad_id'] = $us;
                        header('location:' . DEF_AD_URL);
                    } else {
                        throw new Exception('err');
                    }
                } catch (Exception $exc) {
                    $data['error'] = $this->view->a_lan['err'];
                }
            } else {
                $data['error'] = $this->view->a_lan['err'];
            }
        }
        $this->view->get_View('login.php', $data);
    }

    public function home_view($act) {
        $this->view->get_View('left.php', array('act' => $act));
    }

    public function admin() {
        $this->page = 'ads';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_admin();
        $this->view->get_View('modules/page.php', array('a_table' => array('admin', 'id'), 'mod' => 'admin', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => array('id', 'Tên đăng nhập', 'pass'), 'delete' => true, 'edit' => false));
    }

    public function hoa_don() {
        $this->page = 'bill';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_bill();
        $this->view->get_View('modules/page.php', array('a_table' => array('bill', 'bill_id'), 'mod' => 'hóa đơn', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => array('id', 'Ngày đặt', 'Email', 'Gia'), 'delete' => true, 'edit' => true));
    }

    public function the_loai() {
        $this->page = 'cate';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_cate();
        $this->view->get_View('modules/page.php', array('a_table' => array('categories', 'id'), 'mod' => 'Thể loại', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => array('id', 'Tên thể loại', 'FILE'), 'delete' => true, 'edit' => true));
    }

    public function tin_tuc() {
        $this->page = 'news';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_news();
        $this->view->get_View('modules/page.php', array('a_table' => array('news', 'id'), 'mod' => 'tin tức', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => array('id', 'Tên bài viết', 'hình', 'ngày'), 'delete' => true, 'edit' => true));
    }

    public function template() {
        $this->page = 'temp';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_temp();
        $this->view->get_View('modules/page.php', array('a_table' => array('template', 'id'), 'mod' => 'Template', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => array('id', 'Keywords', 'Hình', 'Hình nhỏ', 'Tên thể loại'), 'delete' => true, 'edit' => true));
    }

    public function customer() {
        $this->page = 'customer';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_customer();
        $this->view->get_View('modules/page.php', array('a_table' => array('customer', 'id'), 'mod' => 'Hóa đơn(Người dùng)', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => $this->view->a_lan['customer'], 'delete' => true, 'edit' => true));
    }

    public function project() {
        $this->page = 'project';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_temp_spec();
        $this->view->get_View('modules/page.php', array('a_table' => array('template', 'id'), 'mod' => 'Template', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => array('id', 'Keywords', 'Hình nhỏ', 'Tên thể loại'), 'delete' => true, 'edit' => true));
    }

    public function menu() {
        $this->page = 'menu';
        $this->home($this->page);
        ad_controller::get_lib('table');
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $data = $at->ad_menu();
        $this->view->get_View('modules/page.php', array('a_table' => array('web_info', 'id'), 'mod' => 'Menu', 'tbl_data' => $data, 'page' => $this->page, 'tbl_head' => array('id', 'Tên menu'), 'delete' => true, 'edit' => true));
    }

    public function thong_tin() {
        $this->page = 'wi';
        $this->home($this->page);
        ad_controller::get_lib('tbl_add');
        ad_controller::get_model('ad_table');
        $this->view->get_View('modules/info.php', array('web_def', $this->view->a_lan, '0'));
    }

    public function delete() {
        $url = new Url(MAIN_AD_SEG);
        ad_controller::get_model('ad_table');
        $at = new ad_table();
        $seg = $url->segment(2);
        $test = $at->del_ever($seg, $url->segment(3), $url->segment(4));
        if ($test) {
            header('location:' . DEF_URL . $seg);
        } else {
            echo 'die';
        }
    }

    function tbl_add() {
        $url = new Url(MAIN_SEG);
        $seg4 = $url->segment(4);
        $this->home($seg4);
        echo $seg4;
        $seg = $url->segment(3);
        ad_controller::get_model('ad_table');
        if (isset($seg)) {
            echo '<div id="Right">';
            ad_controller::get_lib('tbl_add');
            if ($seg4 == 'project') {
                new tbl_add($seg, $this->view->a_lan, $url->segment(5), array(), array('file_temp' => '', 'price' => '','temp_date'=>''));
            }else if($seg4 == 'template'){
            	new tbl_add($seg, $this->view->a_lan, $url->segment(5), array(), array('temp_date'=>'','zip_temp'=>''));
            } else {
                new tbl_add($seg, $this->view->a_lan, $url->segment(5));
            }
            echo '</div>';
        }
    }

}